Imports ClubStarterKit.Web
Imports ClubStarterKit.Data
Imports System.Linq
Imports System.Data.Linq

Partial Class Statistics_EditScore
    Inherits System.Web.UI.Page

    Dim _matchId As Integer

    Protected Sub UpdateScore_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles UpdateScore.Click
        'Dim team1score As Integer = 0
        'Dim team2score As Integer = 0
        Dim isvalid As Boolean = True
        
        Try

            Using dbcontext As New CricketBLL.CricketStatsDataContext(Settings.DatabaseConnectionString)

                Dim match = (From m In dbcontext.Matches _
                            Where m.MatchId = _matchId _
                            Select m).Single()

                match.TeamOvers = Integer.Parse(txtOvers.Text)
                match.TeamRuns = Integer.Parse(txtScore.Text)
                match.TeamWickets = Integer.Parse(txtWickets.Text)

                match.OppOvers = Integer.Parse(txtOppOvers.Text)
                match.OppRuns = Integer.Parse(txtOppScore.Text)
                match.OppWickets = Integer.Parse(txtOppWickets.Text)

                match.TimeFinished = tpFinished.SelectedTime.ToShortTimeString()
                match.Result = ddlResult.SelectedItem.Text
                match.ResultComment = txtComments.Text

                dbcontext.SubmitChanges()

            End Using

        Catch ex As Exception
            isvalid = False
            lblError.Text = ex.Message
        End Try

        If isvalid = True Then
            lblError.Visible = False
            UpdateScore.Visible = False
            'League.UpdateGameScore(Integer.Parse(Request("gameid")), team1score, team2score, iscomplete.Checked)
            'Response.Write("<script>window.parent.hidePopWin();</script>")
            'Response.Write("<script>window.close();</script>")
            'ClientScript.RegisterStartupScript(Me.GetType(), "close", "window.close();", True)
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Request("gameid") Is Nothing Then
            Response.Write("<script>window.close()</script>")
        End If
        Try
            _matchId = Integer.Parse(Request("gameid"))
            InitPage()
        Catch ex As Exception
            Response.Write("<script>window.close()</script>")
        End Try
    End Sub

    Private Sub InitPage()
        Using dbcontext As New CricketBLL.CricketStatsDataContext(Settings.DatabaseConnectionString)

            Dim match = (From m In dbcontext.Matches _
                        Where m.MatchId = _matchId _
                        Select m).Single()

            lblOppName1.Text = match.OpponentTeam
            lblOppName2.Text = lblOppName1.Text
            lblOppName3.Text = lblOppName1.Text

            txtComments.Text = match.ResultComment
            txtOppOvers.Text = IIf(match.OppOvers.HasValue, match.OppOvers.ToString(), "")
            txtOppScore.Text = IIf(match.OppRuns.HasValue, match.OppRuns.ToString(), "")
            txtOppWickets.Text = IIf(match.OppWickets.HasValue, match.OppWickets.ToString(), "")
            txtOvers.Text = IIf(match.TeamOvers.HasValue, match.TeamOvers.ToString(), "")
            txtScore.Text = IIf(match.TeamRuns.HasValue, match.TeamRuns.ToString(), "")
            txtWickets.Text = IIf(match.TeamWickets.HasValue, match.TeamWickets.ToString(), "")
            If String.IsNullOrEmpty(match.TimeFinished) = False Then
                tpFinished.SelectedTime = DateTime.Parse(match.TimeFinished)
            End If

        End Using
    End Sub

End Class
